package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.*;
import com.ruoyi.system.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import static com.ruoyi.common.utils.YuanJiaoFenUtils.removeAmtLastZero;

@Service(value = "paymentService")
public class PaymentServiceImpl implements PaymentService {
    private static Logger LOGGER = LoggerFactory.getLogger(PaymentServiceImpl.class);





    @Override
    public Map<String, String> xcxPayment(String orderNum, String money, String openId,String type) throws Exception {
        LOGGER.info("【小程序支付】 统一下单开始, 订单编号=" + orderNum);
        BigDecimal bigDecimal = new BigDecimal(money);
        Map<String, String> resMap = this.xcxUnifieldOrder(type,orderNum, PayConfig.TRADE_TYPE_JSAPI, removeAmtLastZero(YuanJiaoFenUtils.yuanToFen(bigDecimal)).toString(), openId);
        SortedMap<String, String> resultMap = new TreeMap<String, String>();
//        if(resMap.get("err_code_des").equals("201 商户订单号重复")){
//            resMap = this.xcxUnifieldOrder(type,CharUtil.getRandomNum(16), PayConfig.TRADE_TYPE_JSAPI, removeAmtLastZero(YuanJiaoFenUtils.yuanToFen(bigDecimal)).toString(), openId);
//        }
        resultMap.put("appId", "wx1d1600aff771b346");
        resultMap.put("timeStamp", PayUtil.getCurrentTimeStamp());
        resultMap.put("nonceStr", PayUtil.makeUUID(32));
        resultMap.put("package", "prepay_id=" + resMap.get("prepay_id"));
        resultMap.put("signType", "MD5");
        resultMap.put("sign", PayUtil.createSign(resultMap, "youdexinlizixunxiaochengxuzhif66"));
        resultMap.put("returnCode", "SUCCESS");
        resultMap.put("returnMsg", "OK");
        LOGGER.info("【小程序支付】统一下单成功，返回参数:" + resultMap);
        return resultMap;
    }

    @Override
    public int xcxNotify(Map<String, Object> map) throws Exception {

        return 0;
    }

    /**
     * 小程序支付统一下单
     */
    private Map<String, String> xcxUnifieldOrder(String type, String orderNum, String tradeType, String payAmount, String openid) throws Exception {
//封装参数
        SortedMap<String, String> paramMap = new TreeMap<String, String>();
        paramMap.put("appid","wx1d1600aff771b346");
        paramMap.put("mch_id", "1630924014");
        paramMap.put("nonce_str", PayUtil.makeUUID(32));
        paramMap.put("body", "测试");
        paramMap.put("out_trade_no", orderNum);
        paramMap.put("total_fee", payAmount);
        paramMap.put("spbill_create_ip", "127.0.0.1");
        paramMap.put("notify_url", type);
        paramMap.put("trade_type", tradeType);
        paramMap.put("openid", openid);
        paramMap.put("sign", PayUtil.createSign(paramMap,"youdexinlizixunxiaochengxuzhif66"));
//转换为xml
        String xmlData = PayUtil.mapToXml(paramMap);
//请求微信后台，获取预支付ID
        String resXml = HttpUtils.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", xmlData);
        LOGGER.info("【小程序支付】 统一下单响应：\n" + resXml);
        return XMLUtil4jdom.doXMLParse(resXml);
    }


}
